
// 遵守pc与移动的命名规范，移动端前面加 m.,就会自动拼接拦截路径,自动响应式匹配pc/移动
const mobilepath = [
    '/m.home',
    '/m.bodyguide',
    '/m.bodyguide/m.commonsense',
    '/m.bodyguide/m.guide',
    '/m.bodyguide/m.habit',
    '/m.chronicdisease',
    '/m.chronicdisease/m.breathe',
    '/m.covidControl',
    '/m.covidControl/m.epidemicInformation',
    '/m.covidControl/m.protection',
    '/m.covidControl/m.regionalPolicies',
    '/m.covidControl/m.variety',
    '/m.medicalLeading',
    '/m.medicalLeading/m.cell',
    '/m.medicalLeading/m.element',
    '/m.medicalLeading/m.gene',
    '/m.medicalLeading/m.immunization',
    '/m.medicalLeading/m.information',
    '/m.medicalLeading/m.light',
    '/m.medicalLeading/m.magnetism',
    '/m.medicalLeading/m.molecule',
    '/m.medicalLeading/m.nature',
    '/m.modernMedical',
    '/m.modernMedical/m.diagnosis',
    '/m.modernMedical/m.guide',
    '/m.modernMedical/m.nurse',
    '/m.modernMedical/m.treatment',
    '/m.prevention',
    '/m.prevention/m.commonsense',
    '/m.prevention/m.contagion',
    '/m.psychology',
    '/m.psychology/m.barrier',
    '/m.psychology/m.behavior',
    '/m.psychology/m.commonsense',
    '/m.searchoutcome',
    '/m.traditionalmedical',
    '/m.traditionalmedical/m.diagnosis',
    '/m.traditionalmedical/m.guide',
    '/m.traditionalmedical/m.nurse',
    '/m.traditionalmedical/m.treatment',
    '/m.traditionalmedicalcare',
    '/m.traditionalmedicalcare/m.acupuncture',
    '/m.traditionalmedicalcare/m.food',
    '/m.traditionalmedicalcare/m.massage',
    '/m.traditionalmedicalcare/m.moxibustion',
    '/m.article',
    '/m.entry',
]
const pcpath = [
    '/home',
    '/bodyguide',
    '/bodyguide/commonsense',
    '/bodyguide/guide',
    '/bodyguide/habit',
    '/chronicdisease',
    '/chronicdisease/breathe',
    '/covidControl',
    '/covidControl/epidemicInformation',
    '/covidControl/protection',
    '/covidControl/regionalPolicies',
    '/covidControl/variety',
    '/medicalLeading',
    '/medicalLeading/cell',
    '/medicalLeading/element',
    '/medicalLeading/gene',
    '/medicalLeading/immunization',
    '/medicalLeading/information',
    '/medicalLeading/light',
    '/medicalLeading/magnetism',
    '/medicalLeading/molecule',
    '/medicalLeading/nature',
    '/modernMedical',
    '/modernMedical/diagnosis',
    '/modernMedical/guide',
    '/modernMedical/nurse',
    '/modernMedical/treatment',
    '/prevention',
    '/prevention/commonsense',
    '/prevention/contagion',
    '/psychology',
    '/searchoutcome',
    '/traditionalmedical',
    '/traditionalmedicalcare',
    '/traditionalmedicalcare/acupuncture',
    '/traditionalmedicalcare/food',
    '/traditionalmedicalcare/massage',
    '/traditionalmedicalcare/moxibustion',
    '/article',
    '/entry',
]
export default function ({ isServer, req, redirect, route }) {
    // 这两个只有不同项目不同端口才用得上
    let pcOrigin = 'http://124.222.236.194:3000'
    let mobileOrigin = 'http://124.222.236.194:61908'
    // console.log('我是中间件');
    let isMobile = ua => {
        return !!ua.match(/AppleWebKit.*Mobile.*/)
    }
    let userAgent = req ? req.headers['user-agent'] : navigator.userAgent || ''
    if (!req) return
    // http不能直接写死，要取出来，可能是https
    const request = req?.headers?.referer?.split('://')[0] || 'http';
    // console.log('req.headers.host', req.headers.host);
    let arr = req.headers.host.split(':')
    // console.log('isMobile', isMobile(userAgent));
    // console.log('pcpath.includes(route.path.toLowerCase())', pcpath.includes(route.path.toLowerCase()));
    // 如果是移动端，但是确访问了pc的路径
    if (isMobile(userAgent) && pcpath.includes(route.path.toLowerCase())) {
        // console.log('重定向到移动端', req.headers.host, route.fullPath)
        //TODO:上线之后还要改
        return redirect(
            `${request}://124.222.236.194:61908` +
            `/m.${route.fullPath.substring(1, route.fullPath.length)}`
        )
        // return redirect(
        //     `${request}://${req.headers.host}` +
        //     `/m.${route.fullPath.substring(1, route.fullPath.length)}`
        // )
    } else if (
        !isMobile(userAgent) &&
        mobilepath.includes(route.path.toLowerCase())
    ) {
        // 如果是pc进了移动
        // 重定向到pc端
        // console.log('重定向到PC端', req.headers.host, route.fullPath)
        return redirect(
            `${request}://124.222.236.194:3000` +
            `/${route.fullPath.substring(3, route.fullPath.length)}`
        )
        // return redirect(
        //     `${request}://${req.headers.host}` +
        //     `/${route.fullPath.substring(3, route.fullPath.length)}`
        // )
    }
    // return isMobile(userAgent) ? redirect(pcOrigin + route.fullPath):redirect(mobileOrigin+route.fullPath)
    // return isMobile(userAgent) ? '' : redirect(pcOrigin + route.fullPath)
    // 使用redirect 重定向到外链需要加上前缀:http / https
}